<?php
/**
 *
 *通知公告控制器
 *
 **/

namespace Admin\Controller;

class NoticeController extends ComController
{

    /*
     * 添加通知公告
     */
    public   function  add(){

        $uid = session('uid');
        $member = M('member');
        $user= $member->where('uid ='.$uid)->find();

        if($user['level'] == 1){

            $sc_id=$uid;

        }else if($user['level']  ==2){

            $sc_id = $user['pid'];

        }else{

            echo '';

        }

         if(IS_POST){
         	
             $title =   I('post.title','','strip_tags') ? I('post.title','','strip_tags') : '';
             $intro =   I('post.intro','','strip_tags');
             $content =   I('post.content','','') ? I('post.content','','') : '';
             $picture =   I('post.picture') ? I('post.picture') : '';

             if(!$title) $this->error('请填写公告标题');
             if(!$content) $this->error('请填写公告内容');

             $data['title']=$title;
             $data['content']=$content;
             $data['picture']=$picture;
             $data['intro']=$intro;
             $data['sc_id']=$sc_id;
             $data['add_time']=time();
             $data['update_time']=time();
             $notice = M('notice');
             $result= $notice->add($data);
             if($result){
                 $this->success('添加通知公告成功！',U('index'));
             }else{
                 $this->error('添加通知公告失败!');
             }

         }else{

             $this->display();

         }


    }


    /*
     * 通知公告列表
     */
    public    function   index(){

        $uid = session('uid');
        $member = M('member');
        $user= $member->where('uid ='.$uid)->find();

        if($user['level'] == 1){

            $sc_id=$uid;

        }else if($user['level']  ==2){

            $sc_id = $user['pid'];

        }else {

            echo '';
        }

         //搜索分页
        $p= intval($_GET['p'])?$_GET['p']:1;
        $notice = M('notice');
        $pagesize = 6;#每页数量
        $offset = $pagesize * ($p - 1);//计算记录偏移量
        $prefix = C('DB_PREFIX');

        $where = 'sc_id ='. $sc_id;

        //公告标题
        $title = isset($_GET['title']) ? htmlentities($_GET['title']) : '';

        if ($title) {
            $where .= "  and  title like '%{$title}%' ";
        }

        $data=$notice->where($where)
				        ->field('id,title,intro,picture,add_time,content')
				        ->order('add_time desc')
				        ->limit($offset.','.$pagesize)
				        ->select();
        if(!empty($data)){
        	foreach ($data as $k=>$v){
        		if(empty($v['picture'])){
        			//如果图片为空，则从文章中读取第一张图片
        			$pattern="/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg]))[\'|\"].*?[\/]?>/";
        			preg_match($pattern,$v['content'],$match);
        			if(!empty($match)){
        				$data[$k]['picture']=$match[1];
        				unset($data[$k]['content']);
        			}else{
        				//如果正文中也没有图片，则拿幼儿园图片
        				$pic=M('member')->where("uid=$sc_id")->field('head')->find();  
        				$data[$k]['picture']=$pic['head'];   
        				unset($data[$k]['content']);
        			}
        		}
        	}
        }
        $count=$notice->where($where)->count();

        $page = new \Think\Page($count, $pagesize);
        $page = $page->show();
        $this->assign('data',$data);
        $this->assign('page', $page);
        $this->display();

    }


    /*
     * 修改公告
     */

    public   function  edit(){

        $uid = session('uid');
        $member = M('member');
        $user= $member->where('uid ='.$uid)->find();

        if($user['level'] == 1){

            $sc_id=$uid;

        }else if($user['level']  ==2){

            $sc_id = $user['pid'];

        }else{

            echo '';

        }

        $id = I('get.id') ? I('get.id') : 0;

        if(IS_POST){

            $notice = M('notice');
            $res = $notice->where('id ='.$id  and 'sc_id ='.$sc_id)->find();

                if($res){
                	$param['intro'] =  I('post.intro','','strip_tags');
                    $param['title'] =  I('post.title');
                    $param['content'] =  I('post.content','','');
                    $param['picture'] =  I('post.picture');
                    $param['update_time'] = time();

                    $notice = M('notice');
                    $notice->where(array('id'=>$id,'sc_id'=>$sc_id))->save($param);


                    $this->success('修改公告成功！',U('index'));die;

                }else{

                    $this->error('修改公告失败，参数错误！');

                }

        }else{
            $notice = M('notice');
            $data = $notice->where('id ='.$id)->find();
            $this->assign('data',$data);
            $this->display();

        }

    }


    /*
     * 删除公告
     */

    public  function  del(){

        $uid = session('uid');
        $member = M('member');
        $user= $member->where('uid ='.$uid)->find();

        if($user['level'] == 1){

            $sc_id=$uid;

        }else if($user['level']  ==2){

            $sc_id = $user['pid'];

        }else{

            echo '';

        }

        $id = I('get.id') ? I('get.id') : 0;

        $notice = M('notice');
        $data = $notice->where('id ='.$id  and 'sc_id ='.$sc_id)->find();

        if($data){

                $res = $notice->where(array('id'=>$id,'sc_id'=>$sc_id))->delete();

                if($res){
                    $this->success('删除公告，成功!',U('index'));
                }else{
                    $this->error('删除公告，失败!'.$notice->getError());
                }

        }else{

            $this->error('删除公告失败，没有找到该记录，参数错误！');

        }

    }










}